package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.model.Users;
import com.service.UsersService;
import com.util.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 账户表 前端控制器
 * </p>
 *
 * @author Damon
 * @since 2021-04-29
 */
@RestController

@RequestMapping("/users")
public class UsersController {
    Users users = new Users();
    @Autowired
    private UsersService usersService;
    private String time;
    //用户登录
    @RequestMapping("login")
    public DataResult login(String account, String password, String type, HttpSession session) {

        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.select().eq("user_account", account).eq("password", password).eq("type",type);
        Users loginuser = usersService.getOne(wrapper);
        if (loginuser == null) {
            return DataResult.failed().message("用户或者密码错误！");
        }
        else {
            if (type.equals("0")){
                session.removeAttribute("sessionName");
                session.setAttribute("sessionName",loginuser);
                return DataResult.finished().data("data",loginuser).message("登录成功！");
            }else if (type.equals("1")){
                session.setAttribute("sessionName",loginuser);
                return DataResult.finished().data("data",loginuser).message("登录成功！");
            }else if (type.equals("2")){
                session.setAttribute("sessionName",loginuser);
                return DataResult.finished().data("data",loginuser).message("登录成功！");
            }else {
                session.removeAttribute("sessionName");
                session.setAttribute("sessionName",loginuser);
                return DataResult.finished().data("data",loginuser).message("登录成功！");
            }

        }
    }

    @RequestMapping("register")
    public boolean register(Users users){
        usersService.save(users);
        return true;
    }

    @RequestMapping("getsession")
    public Object getsession( HttpSession session){
        return session.getAttribute("sessionName");
    }

}

